package com.monster.service;

import com.monster.dao.*;
import com.monster.pojo.*;
import com.monster.util.DataBaseHtml;

import java.util.ArrayList;
import java.util.List;

public class InfoServiceImpl implements InfoService {
    SingerDao singerDao = new SingerDaoImpl();
    private CommentDao cd = new CommentDaoImpl();

    /**
     * 通过歌手ID查询  歌手信息（歌手名字、歌手简介、歌手海报、歌曲数量、专辑数量、歌曲集合、专辑集合）
     */
    public Singer selectSingerBySingerId(Long singerId) {
        Singer singer = new Singer();
        singer = singerDao.getSingersBySingerId(singerId);
        //专辑数量添加到singer对象
        singer.setSongAmount(singerDao.songsAlbumsAmount(singerId).getSongAmount());
        //歌曲数量添加到singer对象
        singer.setAlbumAmount(singerDao.songsAlbumsAmount(singerId).getAlbumAmount());
        return singer;
    }
    /**
     * 通过歌手ID=foucs_id，关注类型=foucs_type=1查询  歌手关注度
     */
    public int selectFocusBySingerId(Long singerId){
        return singerDao.selectFocusBySingerId(singerId);
    }


    /**
     * 通过专辑ID，查询专辑的详细信息
     * （专辑名字、专辑海报、歌手名字、专辑流派、专辑语种、专辑简介、发行时间、专辑歌曲、
     * 歌曲的ID、歌曲的名字、歌曲时长、）
     */
    public Album selectAlbumByAlbumId(Long albumId) {
        Album album = new Album();
        AlbumDao albumDao = new AlbumDaoImpl();
        //专辑信息
        album = albumDao.findSongsbyAlbumId(albumId);
        //歌曲集合
        List<Song> songList = albumDao.selectSongByAlbumId(albumId);
        album.setSongs(songList);
        //歌曲数量
        int songCount = albumDao.findSongCountByAlbumId(albumId);
        album.setSongCount(songCount);
        //歌手信息
        ArrayList<Singer> singers = albumDao.findSingersByAlbumId(albumId);
        album.setSingers(singers);
        return album;
    }
    /**
     * 通过歌手ID  查询歌手的所有专辑
     */
    public List<Album> selectAlbumBySingerId(Long singerId){
        return  singerDao.selectAlbumBySingerId(singerId);
    }
    /**
     * 通过类型关键字和ID查找对应评论
     */
    public ArrayList<Comment> selectCommentByTypeId(String key, Long id){
        UserDao userDao = new UserDaoImpl();
        int type = -1;
        switch (key) {
            case "song":
                type = 1;
                break;
            case "album":
                type = 3;
                break;
            case "playlist":
                type = 2;
                break;
            default:
        }
        //调用查找专辑对应的评论
        ArrayList<Comment> list = cd.listCommentsByTypeId(id, type);
        for(Comment commentList:list){
            commentList.setUser(userDao.getUserById(commentList.getUser().getId()));
            commentList.setContent(DataBaseHtml.toHtmlProfile(commentList.getContent()));
        }
        return list;
    }

    @Override
    public Comment getCommentByCommentId(Long commentId) {
        return cd.getCommentByCommentId(commentId);
    }

    /**
     * 通过歌单ID   查询歌单的详细信息 （歌单名字、歌单创建用户）
     */
    public Playlist selectPlaylistByPlaylistId(Long playlistId) {
        PlaylistDao playlistDao = new PlaylistDaoImpl();
        //查询歌单信息（歌单ID、歌单名字、创建用户ID、播放量、歌单简介、歌单海报、创建用户名字）
        Playlist playlist = playlistDao.getPlaylistByPlaylistId(playlistId);
        //获取歌单歌曲数量，set到歌单对象里面
        int SongCount = playlistDao.getSongCount(playlistId);
        playlist.setSongCount(SongCount);

        //通过歌单ID，查询歌单里面的歌曲
        ArrayList<Song> songList =playlistDao.selectSongIdByPlaylistId(playlistId);

        //通过歌单ID获取歌单里面的歌，通过歌的ID获取歌手信息
        for(Song newSongList:songList){
            SongDao songDao = new SongDaoImpl();
            //获取歌的ID  newSongList.getSongId()
            newSongList.setSongId(newSongList.getSongId());
            //通过歌的ID获取歌手信息  （使用的时候循环singer  list.getSingers()）
            ArrayList<Singer> singer = songDao.listSingersBySongId(newSongList.getSongId());
            //把歌手信息set到songlIst中
            newSongList.setSingers(singer);
        }

        for(Song newSongList:songList){
            //通过专辑ID查询专辑名字
            AlbumDao albumDao = new AlbumDaoImpl();
            newSongList.setAlbum(albumDao.findSongsbyAlbumId(newSongList.getAlbum().getAlbumId()));
        }
        playlist.setSongs(songList);

        //通过歌单ID查询对应的标签集合
        ArrayList<Tag> tagList = playlistDao.selectTagByPlaylistId(playlistId);
        playlist.setTags(tagList);

        //通过歌单ID、收藏类型=歌单，获取歌单的收藏数量
        Long count = playlistDao.selectCollectAmountByPlaylistId(playlistId);
        playlist.setCollectAmount(count);

        //通过歌单ID 获取评论集合
        ArrayList<Comment> list = this.selectCommentByPlaylistId(playlistId);
        playlist.setComments(list);

        //通过歌单ID 获取标签集合
        TagDao td = new TagDaoImpl();
        playlist.setTags(td.listTagsByPlaylistId(playlistId));

        return playlist;
    }
    /**
     * 通过歌单ID=type_id  comment_type=2(2代表歌单评论的类型)  查询歌单ID对应评论总条数
     */
    public Long selectCommentCountByPlaylistId(Long playlistId){
        PlaylistDao playlistDao = new PlaylistDaoImpl();
        return playlistDao.selectCommentCountByPlaylistId(playlistId);
    }
    /**
     * 通过歌单ID查找对应歌单评论集合
     */
    public ArrayList<Comment> selectCommentByPlaylistId(Long playlistId){
        CommentDao commentDao = new CommentDaoImpl();
        //通过歌单id  评论类型查询对应的歌单评论
        ArrayList<Comment> list = commentDao.listCommentsByTypeId(playlistId,2);
        for(Comment playlistComment:list){
            UserDao userDao = new UserDaoImpl();
            playlistComment.setUser(userDao.getUserById(playlistComment.getUser().getId()));
        }
        return list;
    }
}
